<%@page import="study.top.jsp.PageHandler"%>
<%@page import="study.top.mydb.items.ReplyItem"%>
<%@page import="study.top.mydb.Reply"%>
<%@page import="study.top.jsp.MultipartBox.FileInfo"%>
<%@page import="java.util.ArrayList"%>
<%@page import="study.top.common.DateTime"%>
<%@page import="study.top.mydb.items.BoardItem"%>
<%@page import="study.top.mydb.Board"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page session="true" %>
<%@ include file="dbcon.jsp" %>
<%@ include file="topcategory.jsp" %>

<%
	db.connect();
	int nowPage = web.getInt("repage");
	int seq = web.getInt("seq");
	

	if(seq<0) {
		web.goPage(null, "파라미터가 없습니다.");
		db.disconnect();
		return;
	}
	
	Board board = new Board();
	BoardItem b_item = board.read(seq);
	
	Reply reply = new Reply();
	ArrayList<ReplyItem> rp_list = reply.list(nowPage, seq);
	
	db.disconnect();
	PageHandler p_handler = PageHandler.getInstance();

	if(b_item==null) {
		web.goPage(null, "게시물 데이터를 불러오는데 실패했습니다.");		
		return;
	}


%>
<!DOCTYPE html>
<html lang="ko">
	<%@include file="topHead.jsp"%>

	<body>
	<div class="top-tab top-hidden-xs">
		<%@include file="tab.jsp"%>
	</div>
	<!-- .modal -->
		<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<!-- .modal-dialog -->
			<form class="modal-dialog" name="delete_form" method="post" action="uBrd_delete_ok.jsp" role="form">
				<input type="hidden" name="seq" value="<%=seq%>" />
				<!-- .modal-content -->
				<div class="modal-content">
					<!-- 제목 -->
					<div class="modal-header">
						<!-- 닫기버튼 -->
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="modal-title" id="myModalLabel">게시물 삭제</h4>
					</div>
					<!-- 내용 -->
					<div class="modal-body">
						<h4>정말 게시물을 삭제하시겠습니까?</h4>
						
					</div>
					<!-- 하단 -->
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">
							취소
						</button>
						<button type="submit" class="btn top-btn-custom">
							삭제
						</button>
					</div>
				</div>
				<!-- /.modal-content -->
			</form>
			<!-- /.modal-dialog -->
		</div>
		<!-- /.modal -->
			<div class="container" style="width: 70%">
		
			<div class="page-header">
				<h3>Q&A</h3>
			</div>
	
			<form class="form-horizontal" role="form" action="uBrdQAedit.jsp" method="post">
				<input type="hidden" name="seq" value="<%=seq%>" />
				
				<fieldset>			
				
				
				<div class="table-responsive">
			<table class="table table-striped table-hover ">
				<thead>
					<tr>
						<th>
							<h4><%=b_item.getTitle()%></h4>
							<div class="pull-right hidden-xs">
								<%=DateTime.getInstance().getDateTimeString(b_item.getReg_date())%>
							</div>
						</th>
								
					</tr>
				</thead>
				
				
				<tbody>
					<tr class="text-center">
						
						<td class="text-left">
							<p>
								글쓴이 : 
								<a class="text-left" href=""><%=b_item.getMemberID() %></a>
								
							</p>
						</td>
					
					</tr>
					
					<tr class="text-center">
						
						<td class="text-left">
						
							<% 
	ArrayList<FileInfo> imageList = b_item.getImageList();
	if (imageList != null) {
		for (int i = 0; i < imageList.size(); i++) {
			FileInfo info = imageList.get(i);
			String src = "download.jsp?" + info.toString("utf-8");
%>
							<p class="form-control-static">
							<a href="<%=src%>" data-lightbox="roadtrip" title="<%=b_item.getTitle()%>"><img src="<%=src%> class="img-thumbnail img-resonsive" /></a></p>
<%
		}
	}
%>
							<p class="form-control-static"><%=b_item.getContent()%></p>
						</td>
					</tr>
				
				</tbody>
			</table>
		</div>
				
						
					
					<!-- 입력양식 -->
					<div class="form-group">
						<div class="col-md-12">

						</div>
					</div>
					<!--// 입력양식 -->
					
					<!-- 입력양식 -->
					<div class="form-group">
						<div class="col-md-12">
						
							<div class="col-md-12">
						
								<div class="pull-right">
								<%
									if(mem_seq == b_item.getMember_seq() || mem_seq == 1){
								%>					
									<button type="submit" class="btn top-btn-custom ">수정</button>
								<a data-toggle="modal" href="#myModal" class="btn top-btn-custom">삭제</a>
								<a href="uBrdQAlistMe.jsp" class="btn top-btn-custom">나의 Q&A</a>
								<%
									}
								%>
									<a href="uBrdQAlist.jsp" class="btn top-btn-custom">목록 가기</a>
								
								
									
								</div>
							</div>
						</div>
					</div>
					<!--// 입력양식 -->
					
					
					
				</fieldset>
			</form>
			<br/><br/>
			<button type="button" id="replyshow" class="btn btn-default">댓글 숨기기</button>
			<hr/>
	
<!--  댓글 리스트 시작 -->
									
			<div id="replylist" class="container" style="width: 95%">
			
				<%
					if(rp_list != null) {
						for(int i=0; i<rp_list.size(); i++) {
							ReplyItem ritem = rp_list.get(i);
				%>
					<input type="hidden" name="re_seq" value="<%=ritem.getSeq()%>" />
					<table class="table-condensed">
					
						<tbody>					
							<tr>
								<td>
									<strong style="font-size: 17px;"><%=ritem.getMemberID() %></strong>		
								</td>							
							</tr>
						
						<div class="pull-right ">
						<%
									if(mem_seq == ritem.getMember_seq() || mem_seq ==1){
						%>	
							<form class="form-horizontal" role="form" action="uBrdReplyDelok.jsp" method="post">
								<input type="hidden" name="seq" value="<%=seq%>" />
								<input type="hidden" name="re_seq" value="<%=ritem.getSeq()%>" />
								<button type="submit"  class="btn btn-default" >삭제</button>
							</form>
								<%
									}
						%>				
						</div>	
						
							<tr>
								<td><%=ritem.getContent() %></td>											
							</tr>				
						</tbody>
											
					</table>
					
						
												
				<div class="pull-right hidden-xs">
					<%=DateTime.getInstance().getDateTimeString(ritem.getReg_date())%>	
				</div>
				
				<hr/>
				<%
						}	
					} else {
				%>
					<table class="table-condensed">
						<tbody>					
							<tr>
								<td>작성한 댓글이 없습니다.</td>							
							</tr>													
						</tbody>
											
					</table><hr/>
				<%
					}
				%>
					
					<!-- 페이지 구현 -->
	<div class="text-center">
		<ul class="pagination pagination-centered">
		
<%
	// 이전 페이지 그룹 링크
	// --> 현재 페이지가 1 페이지의 그룹 수 보다 작다면 첫 그룹이므로
	//	   링크 비 활성화
	if (p_handler.getNowPage() <= p_handler.getPageGroupCount()) {
%>
			<li class="disabled"><a href="#">&laquo;</a></li>
<% 
	} else {
		// 그렇지 않다면, 이전 페이지 그룹의 마지막 페이지로 링크 생성
		int prevPage = p_handler.getStartPage()-1;
%>
			<li><a href="uBrdQAView.jsp?seq=<%=seq %>&repage=<%=prevPage%>">&laquo;</a></li>
<%
	}
%>	
<%
	// 현재 페이지 그룹의 첫 페이지부터 마지막 페이지까지 반복 처리
	for (int i = p_handler.getStartPage(); i <= p_handler.getEndPage(); i++) {
		String classAttr = "";
		if (i == p_handler.getNowPage()) {
			classAttr = "class='active'";
		}	
%>					
			<li <%=classAttr%>><a href="uBrdQAView.jsp?seq=<%=seq %>&repage=<%=i%>"><%=i%></a></li>
<%
	}
%>
<%
	// 현재 페이지가 첫 마지막 페이지 그룹의 첫 페이지보다 크거나 같다면,
	// 다음 페이지로 이동할 수 없다.
	if (p_handler.getNowPage() >= p_handler.getLastGroupFirstPage()) {
%>
			<li class="disabled"><a href="#">&raquo;</a></li>
<%		
	} else {
		// 그렇지 않다면, 현재 그룹의 마지막페이지 +1 --> 다음 그룹의 첫 페이지로
		// 이동할 링크 생성
		int nextPage = p_handler.getEndPage()+1;
%>		
			<li><a href="uBrdQAView.jsp?seq=<%=seq %>&repage=<%=nextPage%>">&raquo;</a></li>
<% 
	}
%>		
		</ul>
	</div>



			<!--// 페이지 구현 -->
										
				</div>
<!-- // 댓글 리스트 끝 -->

		<form id="replyadd" class="form-horizontal" role="form" action="uBrdReplyok.jsp" method="post">
				<input type="hidden" name="seq" value="<%=seq%>" />
				<fieldset>
		
					<!-- 입력양식 -->
					<div class="form-group">
						<div class="col-md-12">
							
						
								<textarea id="content" name="content" class="form-control" rows="5" placeholder="내용을 입력하세요."></textarea>
							
								<button id="btnaddshow" type="submit" class="btn top-btn-custom">댓글남기기</button>	
							
						</div>
					</div>
					<!--// 입력양식 -->
					
					<!-- 입력양식 -->
					<div class="form-group">
						<div class="col-md-offset-2 col-md-10">
				
						</div>
					</div>
					<!--// 입력양식 -->
				</fieldset>
	
			</form>

		</div>
		<div class="top-hidden-xs">
			<%@ include file="topBottom.jsp" %>
		</div>
	
				<!-- Javascript -->
				
				<script src="assets/js/string.js"></script>
				<script src="assets/js/lightbox-2.6.min.js"></script>
				<script src="assets/js/jquery.tmpl.min.js"></script>
				<script src="assets/js/jquery.xml2json.js"></script>
				<link rel="stylesheet" type="text/css" href="assets/css/lightbox.css"/>
				<script type="text/javascript">
				
				
				
				
			$(function(){
				
				var onoff = true;
	
				$('#replylist').show();
								
				$('#replyshow').click(function(){
					
					if(onoff){
						$('#replylist').hide();
						$('#replyshow').html("댓글 보이기");
						onoff=false;
					}else {
						$('#replylist').show();
						$('#replyshow').html("댓글 숨기기");
						onoff=true;
					}
					
					
				});
				
				$("#replyadd").submit(function(){
									
					if (!$("textarea[name='content']").val()
							.isValue()) {
						alert("내용을 입력하세요.");
						$("textarea[name='content']").focus();
						return false;
					}
							
				});
	
			});

		</script>
				
	</body>
</html>